package ut.servlet;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
public class SecureCode extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String code = Integer.toString((int)(Math.random() * 8998 + 1000));
		request.getSession().setAttribute("code", code);

		response.setContentType("image/jpeg");
		ServletOutputStream output = response.getOutputStream();
		BufferedImage img = new BufferedImage(40, 30, BufferedImage.TYPE_INT_RGB);
		Graphics2D gs = img.createGraphics();
		gs.setColor(Color.black);
		gs.fillRect(0, 0, 40, 30);
		gs.setColor(Color.WHITE);
		gs.drawString(code, 5, 20);
		gs.drawImage(img, null, 0 , 0);
		gs.dispose();
		ImageIO.write(img, "jpeg", output);
		output.flush();
		output.close();
	}
}